//
//  MyDynamicViewController.m
//  Findif
//
//  Created by Mac on 2019/3/25.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "MyDynamicViewController.h"
#import "SquareTableViewCell.h"
#import "SquareModel.h"
#import "SquareDetailViewController.h"
#import "MapViewController.h"
#import "GroupInfoViewController.h"

@interface MyDynamicViewController ()<UITableViewDelegate,UITableViewDataSource,SquareTableViewCellDelegate>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <SquareModel *> * listArray;
@property (nonatomic, weak)UIImageView * spaceImageView;
@property (nonatomic, weak)UILabel * spaceLabel;

@end

@implementation MyDynamicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"我的动态";
    [self setupUI];
    [self loadData];
}

- (void)loadData{
    
    NSString * user_id = DEF_GET_OBJECT(@"id");
    
    NSDictionary * dict = @{@"user_id":user_id};
    
    [[ZRNetworkTool sharedTool] getMyDynamicDict:dict WithBlock:^(BOOL success, id responseData) {
        
        //        NSLog(@"res===%@",responseData);
        if (success) {
            
            self.listArray = [NSArray yy_modelArrayWithClass:[SquareModel class] json:responseData[@"data"]];
        }
    }];
    
    [self.tableView.mj_header endRefreshing];
}


- (void)setListArray:(NSArray<SquareModel *> *)listArray{
    
    _listArray = listArray;
    
    [self.tableView reloadData];
}

- (void)setupUI{

    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF4F5F6);
    //    tableView.rowHeight = 124 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[SquareTableViewCell class] forCellReuseIdentifier:@"MyDynamicSquareTableViewCell"];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self loadData];
    }];
    
    [self.view addSubview:self.tableView];
    
    
    UIImageView * spaceImageView = [UIImageView new];
    
    spaceImageView.image = [UIImage imageNamed:@"状态为空"];
    spaceImageView.hidden = YES;
    
    self.spaceImageView = spaceImageView;
    
    [self.tableView addSubview:self.spaceImageView];
    
    
    UILabel * spaceLabel = [UILabel new];
    
    spaceLabel.text = @"您暂时没有发布动态";
    spaceLabel.textColor = [UIColor lightGrayColor];
    spaceLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    spaceLabel.textAlignment = NSTextAlignmentCenter;
    spaceLabel.hidden = YES;
    
    self.spaceLabel = spaceLabel;
    
    [self.tableView addSubview:self.spaceLabel];
}

- (void)viewDidLayoutSubviews{
    
    [self.spaceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.tableView);
        make.centerY.mas_equalTo(self.tableView).offset(-30 * SCREENH6);
        make.width.mas_equalTo(169 * SCREENH6);
        make.height.mas_equalTo(136 * SCREENH6);
    }];
    
    [self.spaceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.spaceImageView);
        make.top.mas_equalTo(self.spaceImageView.mas_bottom).offset(10 * SCREENH6);
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.listArray.count == 0) {
        
        self.spaceImageView.hidden = NO;
        self.spaceLabel.hidden = NO;
    }else{
        
        self.spaceImageView.hidden = YES;
        self.spaceLabel.hidden = YES;
    }
    return self.listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SquareTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行，而不是从cell重用队列中取出
    if (cell == nil) {
        cell = [[SquareTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyDynamicSquareTableViewCell"];
        
    }
    
    cell.delegate = self;
    cell.model = self.listArray[indexPath.row];
    cell.deleteButton.hidden = NO;
    cell.muneButton.hidden = YES;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HiddenBar;
    SquareDetailViewController * datailVC = [SquareDetailViewController new];
    
    SquareModel * model = self.listArray[indexPath.row];
    
    datailVC.model = model;
    
    [self.navigationController pushViewController:datailVC animated:YES];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 使用缓存行高，避免计算多次
    SquareModel * model = self.listArray[indexPath.row];
    return model.rowHeight;
}


- (void)clickLocalButtonLocal:(NSString *)local WithDynamic_latitude:(NSString *)latitude WithDynamic_longitude:(NSString *)longitude{
    
    HiddenBar;
    
    MapViewController * mapVC = [MapViewController new];
    
    mapVC.merchants_latitude = latitude;
    mapVC.merchants_longitude = longitude;
    mapVC.merchants_local = local;
    
    [self.navigationController pushViewController:mapVC animated:YES];

}

- (void)clickGroupLabelWithCell:(SquareTableViewCell *)cell{

    NSDictionary * dict = @{@"id":cell.model.group_id};
    [[ZRNetworkTool sharedTool] checkGroupStatusWithDict:dict WithBlock:^(BOOL success, id responseData) {
        
        //        NSLog(@"res===%@",responseData);
        if (success) {
            
            HiddenBar;
            GroupInfoViewController * infoVC = [GroupInfoViewController new];
            
            infoVC.group_id = cell.model.group_id;
            
            [self.navigationController pushViewController:infoVC animated:YES];
            
            ShowBar;
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}

- (void)clickDeleteButtonWith:(NSString *)ID{
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                   message:@"确定删除该条动态吗？"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              
                                                              [[ZRNetworkTool sharedTool] deleteDynamicWithID:ID WithBlock:^(BOOL success, id responseData) {
                                                                  
                                                                  if (success) {
                                                                      
                                                                      [self loadData];
                                                                  }else{
                                                                      
                                                                      UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                                                                                     message:responseData[@"message"]
                                                                                                                              preferredStyle:UIAlertControllerStyleAlert];
                                                                      
                                                                      UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                                                                            handler:^(UIAlertAction * action) {
                                                                                                                                
                                                                                                                            }];
                                                                      
                                                                      [alert addAction:defaultAction];
                                                                      [self presentViewController:alert animated:YES completion:nil];
                                                                  }
                                                              }];
                                                              
                                                          }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                          handler:^(UIAlertAction * action) {
                                                              
                                                          }];
    
    [alert addAction:defaultAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
    
    
    
    
}

@end
